export default {
	data() {
		return {
			subpages: [{
				title: '',
				value: '',
				created: false,
			}],
			currentPageIndex: 0,
			currentPageName: '',
		}
	},
	computed: {
		subpageValues() {
			return this.subpages.map(item => {
				return item.value;
			});
		}
	},

	// e.g. ?type=payment&p=sharing
	onLoad(options) {
		try {
			const pageValues = this.subpageValues;
			// options.p = 'xid';
			let value = options.p || '';
			if (!pageValues.includes(value)) {
				value = pageValues[0];
			}
			this.showPageWithValue(value);
		} catch (e) {
			//TODO handle the exception
			console.log(e);
		}
	},

	onShow(options) {
		this.currentPage() && typeof this.currentPage().show === 'function' && this.currentPage().show(options);
	},

	onReady() {
		this.currentPage() && typeof this.currentPage().ready === 'function' && this.currentPage().ready();
	},

	onPullDownRefresh() {
		this.currentPage() && typeof this.currentPage().pullDownRefresh === 'function' && this.currentPage()
			.pullDownRefresh();
	},

	onReachBottom() {
		this.currentPage() && typeof this.currentPage().reachBottom === 'function' && this.currentPage().reachBottom();
	},

	onPageScroll(e) {
		this.currentPage() && typeof this.currentPage().pageScroll === 'function' && this.currentPage().pageScroll(e);
	},

	methods: {
		currentPage: function() {
			return this.$refs[this.currentPageName];
		},

		showPageWithValue: function(pageValue) {
			for (var i = 0; i < this.subpages.length; i++) {
				let menu = this.subpages[i];
				if (menu.value == pageValue) {
					this.currentPageIndex = i;
					this.$set(menu, 'created', true);
					break;
				}
			}
			this.currentPageName = this.subpages[this.currentPageIndex].value;
		},

		showPageWithIndex: function(index) {
			try {
				let page = this.subpages[index];
				this.$set(page, 'created', true);
				this.currentPageName = page.value;
				this.currentPageIndex = index;
			} catch (e) {
				//TODO handle the exception
				console.log(e);
			}
		}
	}
}